<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

	<!-- TAGS CLOUD -->
	<xsl:template match="tags" mode="block-tags-cloud">
		<xsl:for-each select="tag">
			<a>
				<xsl:attribute name="href">
					<xsl:value-of select="concat ($baseURI, '/blogs/tag/', value)" />
				</xsl:attribute>
				<xsl:attribute name="style">
					<xsl:value-of select="concat ('font-size: ', size, 'px')" />
				</xsl:attribute>
				<xsl:value-of select="value" />
			</a>
			<xsl:text> </xsl:text>
		</xsl:for-each>
	</xsl:template>
	
	<!-- BLOGS BLOCK -->
	<xsl:template match="response" mode="block-blogs">
		<xsl:if test="count (object) = 0">
			<xsl:text>К сожалению, блогов не нашлось</xsl:text>
		</xsl:if>
		
		<ul class="blogs-top">
			<xsl:apply-templates seelct="response" />
		</ul>
		
		<p style="text-align:right;">
			<a href="{$baseURI}/blogs/top">весь рейтинг</a>
		</p>
	</xsl:template>
	
	<xsl:template match="object[@type='blog']">
		<xsl:param name="alias" select="property[@name='alias']/value" />
		
		<li>
			<div class="rating">
				<xsl:if test="position() = 1">
					<span class="word">рейтинг </span>
				</xsl:if>
				<span class="number">
					<xsl:value-of select="property[@name='rating']/format" />
				</span>
			</div>
			<div class="name">
				<a href="{$baseURI}/blog/{$alias}">
					<xsl:value-of select="property[@name='title']/value" />
				</a>
			</div>
		</li>
	</xsl:template>
	
	<!-- LIVE COMMENTS -->
	<xsl:template match="response" mode="block-live-comments">
		<ul class="live-comments">
			<xsl:apply-templates select="object[@type='comment']" mode="live-comment" />
		</ul>
		
		<p style="text-align:right;">
			<a href="{$baseURI}/comments">весь прямой эфир</a>
		</p>
	</xsl:template>
	
	<xsl:template match="object[@type='comment']" mode="live-comment">
		<xsl:param name="userLogin" select="property[@name='user_login']/value" />
		<xsl:param name="object" select="document(concat('xobject://', @type-name, '/', property[@name='object_id']/value))/data/object" />
		
		<li>
			<a class="user" href="{$baseURI}/user/{$userLogin}">
				<xsl:value-of select="property[@name='user_login']/value" />
			</a>
			
			<xsl:text> → </xsl:text>
			
			<span class="object">
				<xsl:choose>
					<xsl:when test="@type-name='article' or @type-name='link'">
						<a class="object">
							<xsl:attribute name="href">
								<xsl:value-of select="concat($baseURI, '/post/', @type-name, '/', $object/@id, '#comment', @id)" />
							</xsl:attribute>
							<xsl:value-of select="$object/property[@name='title']/value" disable-output-escaping="yes" />
						</a>
						<xsl:text disable-output-escaping="yes"> в&amp;nbsp;блоге </xsl:text>
						<xsl:if test="$object/property[@name='blog_id']/value = 0">
							<a class="category">
								<xsl:attribute name="href">
									<xsl:value-of select="$baseURI" />
									<xsl:text>/blogs/user/</xsl:text>
									<xsl:value-of select="$object/property[@name='user_login']/value" />
								</xsl:attribute>
								<xsl:text>им. </xsl:text>
								<xsl:value-of select="$object/property[@name='user_login']/value" />
							</a>
						</xsl:if>
						
						<xsl:if test="$object/property[@name='blog_id']/value != 0">
							<a class="category">
								<xsl:attribute name="href">
									<xsl:value-of select="$baseURI" />
									<xsl:text>/blog/</xsl:text>
									<xsl:value-of select="$object/property[@name='blog_alias']/value" />
								</xsl:attribute>
								<xsl:value-of select="$object/property[@name='blog_title']/value" />
							</a>
						</xsl:if>
					</xsl:when>
					
					<xsl:when test="@type-name='photo'">
						<a class="object">
							<xsl:attribute name="href">
								<xsl:value-of select="concat($baseURI, '/photo/', $object/@id, '#comment', @id)" />
							</xsl:attribute>
							<xsl:value-of select="$object/property[@name='title']/value" disable-output-escaping="yes" />
						</a>
						<xsl:text disable-output-escaping="yes"> в&amp;nbsp;фотоальбоме </xsl:text>
						<a class="category photoalbum">
							<xsl:attribute name="href">
								<xsl:value-of select="$baseURI" />
								<xsl:text>/photoalbum/</xsl:text>
								<xsl:value-of select="$object/property[@name='album_id']/value" />
							</xsl:attribute>
							<xsl:value-of select="$object/property[@name='album_title']/value" />
						</a>
					</xsl:when>
				</xsl:choose>
			</span>
		</li>
	</xsl:template>
	
	<!-- Block similar posts -->
	<xsl:template match="response" mode="block-similar-posts">
		<xsl:if test="count (object) = 0">
			<xsl:text>К сожалению, похожих публикаций не нашлось</xsl:text>
		</xsl:if>
		
		<xsl:if test="count (object) != 0">
			<ul class="similar-posts">
				<xsl:apply-templates mode="similar-post-item" />
			</ul>
		</xsl:if>
	</xsl:template>
	
	<xsl:template match="object[@type='article' or @type='link']" mode="similar-post-item">
		<xsl:param name="id" select="@id" />
		<xsl:param name="typeName" select="@type" />
		<xsl:param name="userLogin" select="property[@name='user_login']/value" />
		<xsl:param name="blogAlias" select="property[@name='blog_alias']/value" />
		<xsl:param name="blogId" select="property[@name='blog_id']/value" />
		
		<li>
			<span class="date">
				<xsl:value-of select="property[@name='pubdate']/value" disable-output-escaping="yes" />
			</span>
			<xsl:text> → </xsl:text>
			<xsl:if test="$blogId = '0'">
				<a href="{$baseURI}/blogs/user/{$userLogin}" class="blog">
					<xsl:text>блог им. </xsl:text>
					<xsl:value-of select="$userLogin" />
				</a>
			</xsl:if>
			<xsl:if test="$blogId != '0'">
				<a href="{$baseURI}/blog/{$blogAlias}" class="blog">
					<xsl:value-of select="property[@name='blog_title']/value" disable-output-escaping="yes" />
				</a>
			</xsl:if>
			<xsl:text> → </xsl:text>
			<a href="{$baseURI}/post/{$typeName}/{$id}" class="title">
				<xsl:value-of select="property[@name='title']/value" disable-output-escaping="yes" />
			</a>
		</li>
	</xsl:template>

</xsl:stylesheet>